package com.shang.controller;

import com.shang.enums.YesOrNo;
import com.shang.pojo.Carousel;
import com.shang.pojo.Category;
import com.shang.pojo.vo.CategoryVO;
import com.shang.pojo.vo.NewItemsVO;
import com.shang.service.CarouselService;
import com.shang.service.CategoryService;
import com.shang.utils.JsonUtils;
import com.shang.utils.RedisOperator;
import com.shang.utils.ShangJSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Api(value = "首页",tags = {"首页展示的相关接口"})
@RestController
@RequestMapping("index")
public class IndexController {

    @Autowired
    private CarouselService carouselService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedisOperator redisOperator;

    @ApiOperation(value = "首页轮播",notes = "",httpMethod = "GET")
    @GetMapping("/carousel")
    public ShangJSONResult carousel(){
        List<Carousel> list = new ArrayList<>();
        String carouselStr = redisOperator.get("carousel");
        if (StringUtils.isBlank(carouselStr)){
            list = carouselService.queryAll(YesOrNo.YES.type);
            //存入Redis
            redisOperator.set("carousel", JsonUtils.objectToJson(list));
        }else {
            list = JsonUtils.jsonToList(carouselStr,Carousel.class);
        }

        return ShangJSONResult.ok(list);
    }


    @ApiOperation(value = "商品一级分类",notes = "",httpMethod = "GET")
    @GetMapping("/cats")
    public ShangJSONResult cats(){
        List<Category> list = new ArrayList<>();

        String catsStr = redisOperator.get("cats");
        if (StringUtils.isBlank(catsStr)){
            list = categoryService.queryAllRootLevelCat();
            redisOperator.set("cats",JsonUtils.objectToJson(list));
        }else {
            list = JsonUtils.jsonToList(catsStr,Category.class);
        }

        return ShangJSONResult.ok(list);
    }

    @ApiOperation(value = "商品子分类",notes = "",httpMethod = "GET")
    @GetMapping("/subCat/{rootCatId}")
    public ShangJSONResult subCat(@PathVariable Integer rootCatId){

        if (rootCatId == null){
            return ShangJSONResult.errorMsg("");
        }
        List<CategoryVO> list = new ArrayList<>();
        String catsStr = redisOperator.get("subCat:" + rootCatId);
        if (StringUtils.isBlank(catsStr)){
            //查询不存在的数据时 会造成缓存穿透情况 大量请求会绕过Redis直接打在数据库上
            //缓存雪崩   大量Key过期失效 造成大量的并发请求同时到达数据库
//            if (list != null && list.size() > 0){
//                list = categoryService.getSubCatList(rootCatId);
//            }
            list = categoryService.getSubCatList(rootCatId);
            redisOperator.set("subCat:" + rootCatId,JsonUtils.objectToJson(list));
        }else {
            list = JsonUtils.jsonToList(catsStr,CategoryVO.class);
        }

        return ShangJSONResult.ok(list);
    }

    @ApiOperation(value = "商品推荐",notes = "",httpMethod = "GET")
    @GetMapping("/sixNewItems/{rootCatId}")
    public ShangJSONResult sixNewItems(@PathVariable Integer rootCatId){

        if (rootCatId == null){
            return ShangJSONResult.errorMsg("");
        }
        List<NewItemsVO> list = categoryService.getSixNewItemsLazy(rootCatId);

        return ShangJSONResult.ok(list);
    }

}
